﻿
using System;
using System.Collections.Generic;
using Test30.Util;

namespace Test30.Engine
{
    public class GameEvent<TSender, TArgs> : IEvent<TSender, TArgs> where TArgs : EventArgs
    {
        internal void Raise(TSender sender, TArgs args)
        {
            _handlers?.ForEach((action) => action(sender, args));
        }

        #region IEvent<TSender,TArgs> Members
        public void Add(Action<TSender, TArgs> handler)
        {
            if (_handlers == null)
                _handlers = new List<Action<TSender, TArgs>>();
            _handlers.Add(handler);
        }

        public void Remove(Action<TSender, TArgs> handler)
        {
            if (_handlers == null)
                throw new InvalidOperationException("它没有包含任何回调");
            _handlers.Remove(handler);
        }
        #endregion

        private List<Action<TSender, TArgs>> _handlers;
    }
}
